<template>
  <div>
    <el-row class="s-row" type="flex">
      <el-col :span="2" :offset="1">
        <el-button class="s-home-btn" type="text" @click="goHome" icon="el-icon-arrow-left">返回首页</el-button>
      </el-col>
      <el-col :offset="15" :span="6">
        <div class="search-box">
          <el-input class="header-input" v-model="keyWord" placeholder="搜索关键词"></el-input>
          <el-button icon="el-icon-search"  @click="_search"></el-button>
        </div>
      </el-col>
    </el-row>
    <el-card class="search-result">
      <span>共找到{{len}}条结果</span>
      <div v-if="len != 0" style="padding-top:30px">
        <div class="result-card" v-for="(item, index) in video" :key="index">
          <el-image :src="item.img | coverUrl" lazy class="result-pic" @click="selectItem(item)"></el-image>
          <div class="result-title">
            <span v-html="keyHighlight(item.title)"></span>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { search } from "@/api/search";
import { coverUrl } from "@/utils/url";
export default {
  data() {
    return {
      keyWord: "",
      video: [],
      len: 0,
    };
  },
  methods: {
    goHome() {
      this.$router.push({ name: "home" });
    },
    _search() {
      search(this.keyWord).then((res) => {
        if (res.data.code === 200) {
          let newVideos = res.data.data.videos;
          this.len = newVideos.length;
          if (newVideos.length != 0) {
            this.video = res.data.data.videos;
          } else {
            this.$message.info("没有找到");
          }
        }
      });
    },
    selectItem(item) {
      this.$router.push({ name: "video", params: { vid: item.vid } });
    },
    keyHighlight(val) {
      val = val + "";
      // 匹配关键字正则
      let replaceReg = new RegExp(this.keyWord, 'g')
      let replaceString = '<font color="#409EFF">' + this.keyWord + '</font>'    
      return val.replace(replaceReg,replaceString)
    },
    init() {
      this._search();
    },
  },
  created() {
    this.keyWord = this.$route.params.search;
    this.init();
  },
  filters: {
    coverUrl(name) {
      return coverUrl(name);
    },
  },
};
</script>

<style>
.s-row {
  background-color: #2196f3;
  display: flex;
  height: 65px;
}

.s-home-btn {
  margin-top: 10px;
  font-size: 20px;
  color: #fff;
}

.search-box {
  margin: 16px;
}

.header-input {
  width: 80%;
  margin: 0 10px;
}

.search-result {
  margin: auto;
  width: 80%;
  min-height: 800px;
  min-width: 1200px;
}

.result-card {
  border-style:solid;
  border-width:1px;
  border-color:#808080;
  border-radius: 10px;
  display: inline-block;
  width: 22%;
  padding: 10px 5px;
  box-sizing: border-box;
  margin-left: 30px;
  background-color: #fff;
}

.result-pic {
  top: 0px;
  left: 0px;
  width: 100%;
  height: 200px;
}

.result-title {
  height: 20px;
  margin: 0;
  font-size: 18px;
  line-height: 20px;
  color: #212121;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
</style>